package org.yinweichen.llm_dataset_backend.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.yinweichen.llm_dataset_backend.DTO.StandardAnswerCreateDTO;
import org.yinweichen.llm_dataset_backend.DTO.StandardAnswerUpdateDTO;
import org.yinweichen.llm_dataset_backend.entity.LlmAnswer;
import org.yinweichen.llm_dataset_backend.entity.StandardAnswer;
import org.yinweichen.llm_dataset_backend.service.LlmService;
import org.yinweichen.llm_dataset_backend.service.StandardAnswerService;
import org.springframework.http.HttpStatus;

import java.util.List;

@RestController
@RequestMapping("/api/answers")
public class AnswerController {
    private final StandardAnswerService standardAnswerService;
    private final LlmService llmService;

    public AnswerController(StandardAnswerService standardAnswerService, LlmService llmService) {
        this.standardAnswerService = standardAnswerService;
        this.llmService = llmService;
    }

    @PostMapping("/standard")
    public ResponseEntity<StandardAnswer> createStandardAnswer(
            @RequestBody StandardAnswerCreateDTO createDTO) {
        try {
            // 参数验证
            if (createDTO == null || createDTO.answer() == null || createDTO.answer().isEmpty()) {
                return ResponseEntity.badRequest().build();
            }

            return standardAnswerService.create(createDTO);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().build();
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    //TODO: 增加version路径变量
    @GetMapping("/standard/{id}/{version}")
    public ResponseEntity<StandardAnswer> getStandardAnswerById(@PathVariable Long id,@PathVariable String version) {
        return standardAnswerService.getByQuestion(id,version);
    }
    /**
     * 更新标准答案
     * @param standardAnswerUpdateDTO 包含更新后信息的标准答案对象
     * @return ResponseEntity 包含更新后的标准答案或错误信息
     */
    @PutMapping("/standard")
    public ResponseEntity<?> updateStandardAnswer(@RequestBody StandardAnswerUpdateDTO standardAnswerUpdateDTO) {
        try {
            if (standardAnswerUpdateDTO == null || standardAnswerUpdateDTO.answer() == null || standardAnswerUpdateDTO.answer().isEmpty()) {
                return ResponseEntity.badRequest().body("Answer content cannot be empty");
            }
            return standardAnswerService.update(standardAnswerUpdateDTO);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage());
        } catch (RuntimeException e) {
            return ResponseEntity.internalServerError().body(e.getMessage());
        }
    }

    //大模型回答管理

    /**
     * 获取大模型的回答列表
     * @param modelName 大模型名称
     * @param questionId 问题ID
     * @return ResponseEntity<List<LlmAnswer>> 大模型回答列表
     */
    @GetMapping("/llm")//TODO:增加version
    public ResponseEntity<List<LlmAnswer>> getLlmAnswer(
            @RequestParam(required = false) String modelName,
            @RequestParam(required = false) Long questionId,
            @RequestParam(required = false) String version) {
        return llmService.getLlmAnswers(modelName, questionId,version);
    }
}
